package com.mustachio.urlshortener
{
	import com.mustachio.comp.SocialControlPanelToolTip;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.TextArea;
	import mx.managers.PopUpManager;

	public class LinkToolTip extends Sprite
	{
		private var localXStartingPoint:uint;
		private var localYStartingPoint:uint;
		private var localTextWidth:uint;
		private var localTextArea:TextArea;
		private var popUp:SocialControlPanelToolTip;
		private var localLinkVO:LinkVO;
		private var toolTipArray:ArrayCollection;
		
		public function LinkToolTip(incomingLinkVO:LinkVO, incomingTextArea:TextArea)
		{
			//calls the super class constructer
			super();
			
			localLinkVO = incomingLinkVO;
			
			//set the local X and Y starting positions;
			localXStartingPoint = localLinkVO.linkXPosition;
			localYStartingPoint = localLinkVO.linkYPosition;
			
			//set the localTextWidth used to make the invisible tooltip the size of the link
			localTextWidth = localLinkVO.textLength;
			
			//set localTextArea to incomingTextArea, this is the textArea
			//that we are going to use for positioning the tool tips.
			localTextArea = incomingTextArea;
			
			toolTipArray = new ArrayCollection();
			
			//call the function to actually make the tool tips.
			makeTips();
		}
		
		private function makeTips():void{
			popUp = PopUpManager.createPopUp(localTextArea, SocialControlPanelToolTip, false) as SocialControlPanelToolTip;
	       	popUp.addEventListener("undoEvent", undoShortenHandler, false, 0, true);
			popUp.addEventListener("redoEvent", redoShortenHandler, false, 0, true);
	       	popUp.origionalLink = localLinkVO.fullLink; 
	        popUp.x = localTextArea.x + localXStartingPoint;
            popUp.y = (localTextArea.y + localYStartingPoint);
            popUp.width = localTextWidth; 
            
            localLinkVO.toolTipInstanceName = popUp.name;
            
            toolTipArray.addItem(popUp);
		}
		
		private function undoShortenHandler(e:Event):void{
			//var longURL:String = linkChecker.ogLinkArray[0];
			localTextArea.text = localTextArea.text.replace(localLinkVO.shortLink, localLinkVO.fullLink);
		}
			
		private function redoShortenHandler(e:Event):void{
			//var shortURL:String = resultArray[0];
			localTextArea.text = localTextArea.text.replace(localLinkVO.fullLink, localLinkVO.shortLink);
		}
		
		public function removeSpecificToolTip(popUpName:String):void{
			if(toolTipArray.length > 0){
				for (var i:uint = 0; i < toolTipArray.length; i++){
					if(popUpName == toolTipArray[i].name){
						//localTextArea.removeChild(popUp);
						PopUpManager.removePopUp(popUp);
						toolTipArray.removeItemAt(i);
					}
				}
			}
		}
		
	}
}